"""
串口raw数据保存
"""
import datetime
import os

index_str = {1: "01", 2: "02", 3: "03", 4: "04", 5: "05", 6: "06", 7: "07", 8: "08", 9: "09", 10: "10",
             11: "11", 12: "12", 13: "13", 14: "14", 15: "15", 16: "16", 17: "17", 18: "18", 19: "19", 20: "20",
             21: "21", 22: "22", 23: "23", 24: "24", 25: "25", 26: "26", 27: "27", 28: "28", 29: "29", 30: "30",
             31: "31", 32: "32", 33: "33", 34: "34", 35: "35", 36: "36", 37: "37", 38: "38", 39: "39", 40: "40",
             41: "41", 42: "42", 43: "43", 44: "44", 45: "45", 46: "46", 47: "47", 48: "48", 49: "49", 50: "50",
             51: "51", 52: "52", 53: "53", 54: "54", 55: "55", 56: "56", 57: "57", 58: "58", 59: "59", 60: "60"}


class DataSaver:
    def __init__(self, base_dir, frame_size, file_size_m, file_size_h):
        """
        串口数据保存到文件
        :param frame_size: 一个raw文件存储的包数
        :param file_size_m: 每分钟多少个raw文件数
        :param file_size_h: 每小时多少个raw文件数
        """
        self.base_dir = base_dir
        self.frame_size = frame_size
        self.file_size_m = file_size_m
        self.file_size_h = file_size_h

        self.frame_index = 0  # 当前帧数
        self.hour_index = 1  # 下一个文件夹的index
        self.minute_index = 1
        self.file_index = 1
        self.full_path = None
        self.base_folder = None
        self.second_folder = None
        self.third_folder = None
        self.current_file = None
        self.create_base_folder()
        # self.tmp_buffer = bytearray()

    def create_base_folder(self):
        timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H%M%S")
        folder_path = rf"{self.base_dir}\{timestamp}\\"
        os.makedirs(folder_path, exist_ok=True)
        self.base_folder = folder_path
        self.create_second_folder()

    def create_second_folder(self):
        if self.hour_index > self.file_size_h:
            # 超过60个小时文件夹后重置
            self.hour_index = 1
            self.create_base_folder()
            # return
        folder_path = rf"{self.base_folder}\{index_str[self.hour_index]}\\"
        os.makedirs(folder_path, exist_ok=True)
        self.second_folder = folder_path
        self.hour_index = (self.hour_index + 1)
        self.minute_index = 1  # 重置分钟索引
        self.create_third_folder()

    def create_third_folder(self):
        if self.minute_index > self.file_size_h:  # 一小时60个文件
            self.minute_index = 1
            self.create_second_folder()
            # return
        folder_path = rf"{self.second_folder}\{index_str[self.minute_index]}\\"
        os.makedirs(folder_path, exist_ok=True)
        self.third_folder = folder_path
        self.minute_index = (self.minute_index + 1)
        self.file_index = 1
        # self.create_new_file()

    def create_new_file(self, timestamp):
        if self.file_index > self.file_size_m:
            self.file_index = 1
            self.create_third_folder()
            # return
        filename = f"{timestamp}_{index_str[self.file_index]}.raw"
        fullpath = os.path.join(self.third_folder, filename)
        self.full_path = fullpath
        if self.current_file:
            self.current_file.close()
        self.current_file = open(self.full_path, 'ab')
        self.file_index = (self.file_index + 1)  # 下一个文件序号

    def write_to_file(self, tmp_buffer):
        if self.current_file:
            self.current_file.write(tmp_buffer)
            self.frame_index += 20
            if self.frame_index == self.frame_size:
                timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H%M%S%f")[:-3]
                self.create_new_file(timestamp)
                self.frame_index = 0

    def close(self):
        if self.current_file:
            self.current_file.close()
